package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.entity.LensScheduleDowntime;
import com.ds.lens.data.service.dao.sql.provider.LensScheduleDowntimeSqlProvider;
import com.ds.lens.data.vo.in.v2.downtime.LensScheduleListInVO;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface LensScheduleDowntimeDao {

    String COLUMNS = " id, application_id, state, monitor_id, monitor_name, monitor_tags, schedule_type, start_time, end_time, recurring_content, creator, created_time, mender, modified_time, is_delete  ";

    @Update({
            "update lens_schedule_downtime set is_delete=1, mender=#{mender}, modified_time=#{modifiedTime}",
            "where id = #{id,jdbcType=BIGINT} and is_delete = 0"
    })
    int deleteByPrimaryKey(@Param("id") Long id, @Param("mender") String mender,
                           @Param("modifiedTime") Date modifiedTime);


    @Update({
            "update lens_schedule_downtime set is_delete=1, mender=#{mender}, modified_time=#{modifiedTime}",
            "where monitor_id = #{monitorId,jdbcType=BIGINT} and is_delete = 0 and schedule_type = 1 and state = 2"
    })
    int deleteByPrimaryMonitorId(@Param("monitorId") Long monitorId, @Param("mender") String mender,
                                 @Param("modifiedTime") Date modifiedTime);


    @Update({
            "update lens_schedule_downtime set is_delete=1, mender=#{mender}, modified_time=#{modifiedTime}",
            "where monitor_id = #{monitorId,jdbcType=BIGINT} and is_delete = 0 "
    })
    int deleteAllByPrimaryMonitorId(@Param("monitorId") Long monitorId, @Param("mender") String mender,
                                 @Param("modifiedTime") Date modifiedTime);

    @InsertProvider(type = LensScheduleDowntimeSqlProvider.class, method = "insertSelective")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int insertSelective(LensScheduleDowntime record);

    @Select({
            "select",
            COLUMNS,
            "from lens_schedule_downtime",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "scheduleDowntime", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "application_id", property = "applicationId", jdbcType = JdbcType.BIGINT),
            @Result(column = "state", property = "state", jdbcType = JdbcType.TINYINT),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "monitor_name", property = "monitorName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "monitor_tags", property = "monitorTags", jdbcType = JdbcType.VARCHAR),
            @Result(column = "schedule_type", property = "scheduleType", jdbcType = JdbcType.TINYINT),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "recurring_content", property = "recurringContent", jdbcType = JdbcType.VARCHAR),
            @Result(column = "creator", property = "creator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_delete", property = "isDelete", jdbcType = JdbcType.BIT)
    })
    LensScheduleDowntime selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensScheduleDowntimeSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensScheduleDowntime record);

    @Select({
            "<script>",
            "select",
            COLUMNS,
            "from lens_schedule_downtime where is_delete = 0 ",
            "<if test=\"applicationIds!=null and applicationIds.size >0\">",
            " and application_id in ",
            "<foreach collection='applicationIds' item='applicationId' separator=',' open='(' close=')' >",
            "#{applicationId}",
            "</foreach>",
            "</if>",
            "<if test=\"states!=null and states.size > 0\">",
            " and state in ",
            "<foreach collection='states' item='state' separator=',' open='(' close=')' >",
            "#{state}",
            "</foreach>",
            "</if>",
            "<if test=\"monitorName!=null and monitorName != '' \">",
            " and monitor_name like concat(#{monitorName},'%')",
            "</if>",
            "<if test=\"monitorTags!=null and monitorTags.size > 0\">",
            "<foreach collection='monitorTags' item='tag' >",
            " and FIND_IN_SET(#{tag},monitor_tags) ",
            "</foreach>",
            "</if>",
            " order by modified_time desc ",
            "</script>"
    })
    @ResultMap("scheduleDowntime")
    Page<LensScheduleDowntime> queryDowntime(LensScheduleListInVO lensScheduleListInVO);

    @Update({
            "update lens_schedule_downtime set state=#{state} where id = #{id,jdbcType=BIGINT}"
    })
    int updateStateById(@Param("id") Long id, @Param("state") Integer state);


    @Update({
            "update lens_schedule_downtime set state=#{state}, start_time=#{startTime}, end_time = #{endTime}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateRecurringDateById(@Param("id") Long id, @Param("state") Integer state,
                                @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Update({
            "update lens_schedule_downtime set monitor_name=#{monitorName} where monitor_id = #{monitorId,jdbcType=BIGINT}",
            " and is_delete = 0"
    })
    int updateMonitorNameByMonitorId(@Param("monitorId") Long monitorId, @Param("monitorName") String monitorName);

    @Select({
            "select",
            COLUMNS,
            "from lens_schedule_downtime where is_delete = 0"
    })
    @ResultMap("scheduleDowntime")
    List<LensScheduleDowntime> listAllSchedule();
}
